var chess = document.getElementById('chess');
var context = chess.getContext("2d");

//me用来判断是否轮到你下棋， over判断是否已经结束游戏
var me = true;
var over = false;

var chessBoard = [];
for (var i = 0; i < 15; i++) {
	chessBoard[i] = [];
	for (var j = 0; j < 15; j++) {
		chessBoard[i][j] = 0;
	}
}

//赢法数组
var wins = [];
var count = 0;
//win 为三维数组，win[][][0]为第一种赢法
for (var i = 0; i < 15; i++) {
	wins[i] = [];
	for (var j = 0; j < 15; j++) {
		wins[i][j] = [];
	}
}
//横线五个子赢法
for (var i = 0; i < 15; i++) {
	for (var j = 0; j < 11; j++) {
		for (var k = 0; k < 5; k++) {
			wins[i][j + k][count] = true;
		}
		count++;
	}
}
//竖线五个子赢法
for (var i = 0; i < 15; i++) {
	for (var j = 0; j < 11; j++) {
		for (var k = 0; k < 5; k++) {
			wins[j + k][i][count] = true;
		}
		count++;
	}
}
//斜线五个子赢法
for (var i = 0; i < 11; i++) {
	for (var j = 0; j < 11; j++) {
		for (var k = 0; k < 5; k++) {
			wins[i + k][j + k][count] = true;
		}
		count++;
	}
}
//反斜线五个子赢法
for (var i = 0; i < 11; i++) {
	for (var j = 14; j > 3; j--) {
		for (var k = 0; k < 5; k++) {
			wins[i + k][j - k][count] = true;
		}
		count++;
	}
}

//赢法统计数组当myWin[0] 为5时，胜利
myWin = [];
AiWin = [];
for (var i = 0; i < count; i++) {
	myWin[i] = 0;
	AiWin[i] = 0;
}


context.strokeStyle = "#BFBFBF";

var drawChessBoard = function() {
	for (var i = 0; i < 15; i++) {
		//绘制竖线
		context.moveTo(15 + i * 30, 15);
		context.lineTo(15 + i * 30, 435);
		context.stroke();
		//绘制横线
		context.moveTo(15, 15 + i * 30);
		context.lineTo(435, 15 + i * 30);
		context.stroke();
	}
};

var oneStep = function(i, j, me) {
	//绘制圆
	context.beginPath();
	context.arc(15 + i * 30, 15 + j * 30, 13, 0, 2 * Math.PI);
	context.closePath();
	//设置渐变,偏移了圆心2px 是为了显示光线效果
	var gradient = context.createRadialGradient(15 + i * 30 + 2, 15 + j * 30 - 2, 13, 15 + i * 30 + 2, 15 + j * 30 - 2, 0);
	if (me) {
		gradient.addColorStop(0, "#0a0a0a");
		gradient.addColorStop(1, "#636766");
	} else {
		gradient.addColorStop(0, "#d1d1d1");
		gradient.addColorStop(1, "#f9f9f9");
	}
	context.fillStyle = gradient;
	context.fill();
};

drawChessBoard();

chess.onclick = function(e) {
	if (over) {
		return;
	}
	if (!me) {
		return;
	}
	var x = e.offsetX;
	var y = e.offsetY;
	var i = Math.floor(x / 30);
	var j = Math.floor(y / 30);
	if (chessBoard[i][j] == 0) {
		oneStep(i, j, me);
		chessBoard[i][j] = 1;
	}
	me = !me;
	for (var k = 0; k < count; k++) {
		//如果第k种赢法里面有该棋子
		if (wins[i][j][k]) {
			myWin[k]++;
			AiWin[k] = 6; //计算机不可能赢了
			if (myWin[k] == 5) {
				alert("你赢了！");
				over = true;
			}
		}
	}
	if (!over) {
		me = !me;
		computerAI();
	}
};
var computerAI = function() {
	var myScore = [];
	var AiScore = [];
	var max = 0; //保存最高分
	var u = 0,
		v = 0; //保存最高分坐标
	//初始化分数
	for (var i = 0; i < 15; i++) {
		myScore[i] = [];
		AiScore[i] = [];
		for (var j = 0; j < 15; j++) {
			myScore[i][j] = 0;
			AiScore[i][j] = 0;
		}
	}
	//计算分数
	for (var i = 0; i < 15; i++) {
		for (var j = 0; j < 15; j++) {
			if (chessBoard[i][j] == 0) { //该位置没有棋子
				for (var k = 0; k < count; k++) {
					if (wins[i][j][k]) {
						//拦截分数
						if (myWin[k] == 1) {
							myScore[i][j] += 200;
						} else if (myWin[k] == 2) {
							myScore[i][j] += 400;
						} else if (myWin[k] == 3) {
							myScore[i][j] += 2000;
						} else if (myWin[k] == 4) {
							myScore[i][j] += 10000;
						}
						//赢棋分数
						if (AiWin[k] == 1) {
							AiScore[i][j] += 220;
						} else if (AiWin[k] == 2) {
							AiScore[i][j] += 420;
						} else if (AiWin[k] == 3) {
							AiScore[i][j] += 2100;
						} else if (AiWin[k] == 4) {
							AiScore[i][j] += 20000;
						}
					}
				}

				if (myScore[i][j] > max) {
					max = myScore[i][j];
					u = i;
					v = j;

				} else if (myScore[i][j] == max) {
					if (AiScore[i][j] > AiScore[u][v]) {
						u = i;
						v = j;
					}
				}

				if (AiScore[i][j] > max) {
					max = AiScore[i][j];
					u = i;
					v = j;

				} else if (AiScore[i][j] == max) {
					if (myScore[i][j] > myScore[u][v]) {
						u = i;
						v = j;
					}
				}

			}
		}
	}
	oneStep(u, v, false);
	chessBoard[u][v] = 2;

	for (var k = 0; k < count; k++) {
		//如果第k种赢法里面有该棋子
		if (wins[i][j][k]) {
			AiWin[k]++;
			myWin[k] = 6; //我不可能赢了
			if (AiWin[k] == 5) {
				alert("你输了！");
				over = true;
			}
		}
	}
	if (!over) {
		me = !me;
	}

};